package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/login")
    public String login(String name){
        return "接收到参数：" + name;
    }
    @RequestMapping("/r1")
    public String r1(Integer age){
        return "接收到的参数：" + age;
    }
    @RequestMapping("/r2")
    public String r2(int age){
        return "接收到的参数：" + age;
    }
    @RequestMapping("/r3")
    public String r3(String name, String password){
        return "接收到的参数：" + "name:" + name + ", password:" + password;
    }
    @RequestMapping("/r4")
    public String r4(Person person){
        return "接收到的参数：" + person;
    }
    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "q", required = false)String keyword){
        return "搜索词为：" + keyword;
    }
    @RequestMapping("/r6")
    public String r6(String[] arr){
        return "数组:" + Arrays.toString(arr) + ", length:" + arr.length;
    }
    @RequestMapping("/r7")
    public String r7(@RequestParam(required = false) List<String> list){
//        StringBuilder builder=new StringBuilder();
//        for(String s : list){
//            builder.append(s).append(",");
//        }
//        return "集合：" + builder.toString();
        return "集合：" + list;
    }
    @RequestMapping("/r8")
    public String r8(@RequestBody Person person){
        return "Json:" + person;
    }

    @RequestMapping("/article/{articleId}")
    public String r9(@PathVariable("articleId") Integer articleId){
        return "r9 文章ID:" + articleId;
    }

    @RequestMapping("/article/{articleId}/{type}")
    public String r10(@PathVariable("articleId") Integer articleId, @PathVariable String type){
        return "r10 文章ID:" + articleId + ", type:" + type;
    }

    @RequestMapping("/r11")
    public String r11(@RequestPart("file1") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File destFile = new File("D:\\海贼王  yyds\\" + originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "存储文件成功";
    }

    @RequestMapping("/r12")
    public String r12(@RequestParam("file2") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File destFile = new File("D:\\海贼王  yyds\\" + originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "存储文件成功";
    }

    @RequestMapping("getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie:cookies){
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "获取Cookie成功";
    }
}
